<template>
  <div class="websocket-status">
    <el-tooltip :content="statusText" placement="top">
      <div class="status-indicator" :class="statusClass">
        <el-icon><Connection /></el-icon>
      </div>
    </el-tooltip>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { Connection } from '@element-plus/icons-vue'
import { webSocketService } from '../../utils/websocket'

const isConnected = ref(false)
const statusText = ref('未连接')
const statusClass = ref('disconnected')

const updateStatus = () => {
  isConnected.value = webSocketService.getConnectionStatus()
  if (isConnected.value) {
    statusText.value = 'WebSocket已连接'
    statusClass.value = 'connected'
  } else {
    statusText.value = 'WebSocket未连接'
    statusClass.value = 'disconnected'
  }
}

let statusInterval: number

onMounted(() => {
  updateStatus()
  // 每3秒检查一次连接状态
  statusInterval = setInterval(updateStatus, 3000)
})

onUnmounted(() => {
  if (statusInterval) {
    clearInterval(statusInterval)
  }
})
</script>

<style scoped>
.websocket-status {
  display: inline-flex;
  align-items: center;
}

.status-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s;
}

.status-indicator.connected {
  background-color: #67c23a;
  color: white;
}

.status-indicator.disconnected {
  background-color: #f56c6c;
  color: white;
}

.status-indicator:hover {
  transform: scale(1.1);
}
</style>